package team.scau.laboratory_repair.common.activiti.listener;

import lombok.extern.apachecommons.CommonsLog;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import team.scau.laboratory_repair.common.enums.RecordStatus;
import team.scau.laboratory_repair.common.quartz.jobs.repair.SuperviseJob;
import team.scau.laboratory_repair.common.utils.StringTool;
import team.scau.laboratory_repair.modules.po.*;
import team.scau.laboratory_repair.service.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * @author 30254
 * createDate 2018/10/25
 */
@Component
@CommonsLog
public class LabRepairHandlerRe implements TaskListener {

    @Autowired
    private WechatUserService wechatUserService;

    @Autowired
    private UserService userService;

    @Autowired
    private DomainService domainService;

    @Autowired
    private JobService jobService;

    @Autowired
    private AreaService areaService;

    @Autowired
    private RecordService recordService;

    private static final String MESSAGE_TIP = "快登陆微信端网页看看";
    private static final int SUPERVISE_INTERVAL = 10;
    private static final String SUPERVISE_SUBFFIX = "supervise";
    private static final String CHECK_CRON = "* * */2 * * ?";

    @Override
    public void notify(DelegateTask delegateTask) {
        String name = delegateTask.getName();
        switch (name){
            case "处理和跟进":delegate(delegateTask);break;
            default:
        }
    }

    private void delegate(DelegateTask delegateTask){
        // 获取匹配的人员配置并通知
        Long recordId = delegateTask.getVariable("recordId", Long.class);
        Record record = recordService.findById(recordId);
        Area area = areaService.findById(record.getArea());
        Domain domain = domainService.listByAreaAndRoom(record.getArea(), record.getRoom().trim());
        List<Long> longs = StringTool.strings2LongList(domain.getStaffIds());
        List<User> userList = userService.listByIds(longs);
        delegateTask.addCandidateUsers(Arrays.asList(area.getStaffs().split(",")));
        for (User u : userList){
            log.info(u.getPhoneNumber()+RecordStatus.CHECKED.toString()+MESSAGE_TIP);
//            MessageUtil.sendMessageNewItem(u.getPhoneNumber(), RecordStatus.ORIGIN.toString(), MESSAGE_TIP);
        }

        // 开启监督定时任务
        HashMap<String, Object> dataMap = new HashMap<>();
        String phone = delegateTask.getVariable("phone", String.class);
        dataMap.put("phone", phone);
        dataMap.put("recordId", recordId);
        dataMap.put("noAssignee", 0);
        dataMap.put("noProgress", 0);
        dataMap.put("claimed", false);
        try {
            String jobId = recordId + ":" + SUPERVISE_SUBFFIX;
            jobService.addJob(jobId, SuperviseJob.class, "0 0/10 * * * ?", dataMap);
//            jobService.addJob(jobId, SuperviseJob.class, "0 0 */1 * * ?", dataMap);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    public void claim(Long recordId) throws Exception {
        // 更新监督定时任务
        HashMap<String, Object> dataMap = new HashMap<>();
        dataMap.put("claimed", true);
        String jobId = recordId + ":" + SUPERVISE_SUBFFIX;
        jobService.updateJobData(jobId, dataMap);
    }

    public void unclaim(Long staffId, Long recordId) throws Exception {
        // 获取匹配的人员配置并通知其他人
        Record record = recordService.findById(recordId);
        Domain domain = domainService.listByAreaAndRoom(record.getArea(), record.getRoom().trim());
        List<Long> ids = StringTool.strings2LongList(domain.getStaffIds());
        ids.remove(staffId);
        List<WechatUser> userList = wechatUserService.listStaffByIds(ids);
        for (WechatUser u : userList){
            log.info(u.getPhoneNumber()+RecordStatus.CHECKED.toString()+MESSAGE_TIP);
//            MessageUtil.sendMessageNewItem(, , );
        }

        // 更新监督定时任务
        HashMap<String, Object> dataMap = new HashMap<>();
        dataMap.put("claimed", false);
        String jobId = recordId + ":" + SUPERVISE_SUBFFIX;
        jobService.updateJobData(jobId, dataMap);
    }

    public void note(Long recordId) throws Exception {
        // 更新监督定时任务
        HashMap<String, Object> dataMap = new HashMap<>();
        dataMap.put("noProgress", 0);
        String jobId = recordId + ":" + SUPERVISE_SUBFFIX;
        jobService.updateJobData(jobId, dataMap);
    }

    public void complete(Long recordId) throws Exception {
        // 关闭监督定时任务
        String jobId = recordId + ":" + SUPERVISE_SUBFFIX;
        jobService.removeSchedule(jobId);
    }
}
